	import listItem from "@/components/list-item/list-item.vue"
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import listMixins from '@/mixins/listMixins.js'
	import {getList} from '@/api/crud.js'
	export default {
		mixins:[MescrollMixin,listMixins],
		components:{
			listItem
		},
		data() {
			return {
				list:[
					'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3898000000,1367247590&fm=26&gp=0.jpg'
				],
				tabs:[],
				tabIndex: 0, // 当前菜单下标
				top:0,
				show:false,
				selectList:[],
				height:0,
				tabTop:0,
				form:{
					type:'',
					isShow:1,
					uid:'',
					name:'',
					peopleDesc:'',
					types:'script',
					categoryIds:[]
				},
				canReset:false,
				startData:{clientX:'',clientY:''},
				loading:false
			}
		},
		computed: {
			// 列表数据
			tabList() {
				return this.tabs[this.tabIndex]&&this.tabs[this.tabIndex].list || []
			},
			curTab(){
				return this.tabs[this.tabIndex] || {};
			}
		},
		onLoad() {
			const {statusBarHeight,windowHeight} = this.$u.sys();
			this.top = statusBarHeight+44 + 'px'
			this.height = windowHeight 
		},
		onShow() {
			this.setCallback&&this.setCallback()
			this.initCarBus&&this.initCarBus()
		},
		methods: {
			//页面显示 只请求当前tab数据 改为每次显示都重新请求
			setCallback(){
				if(Array.isArray(this.$refs.mescroll)&&this.canReset){
					const arrIndex = this.showRefreshTabIndex&&this.showRefreshTabIndex.split(',') || []; //需要刷新的tabindex数组
					//判断当前tabIndex在不在数组 如果不在则添加进入数组统一处理
					const flag = arrIndex.some(val=>val!=this.tabIndex);
					if(flag){
						arrIndex.push(this.tabIndex)
					}
					arrIndex.forEach(val=>{
						const fn = this.$refs.mescroll[val].selectCallback
						fn&&fn([])
					})
				}
				this.canReset = true;
			},
			/*下拉刷新的回调 */
			downCallback() {
				// 这里加载你想下拉刷新的数据, 比如刷新轮播数据
				// loadSwiper();
				// 下拉刷新的回调,默认重置上拉加载列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
				this.mescroll&&this.mescroll.resetUpScroll()
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				this.loading = true;
				let curTab = this.tabs[this.tabIndex]
				this.form.status = curTab.type;
				this.form.type = curTab.type;
				if(curTab.isLookExhibit) this.form.isLookExhibit = curTab.isLookExhibit
				// 设置列表数据
				const time = new Date().getTime();
				curTab.num = page.num; // 页码
				this.getData(this.name,this.form,page.num).then(res=>{
					if(page.num == 0) curTab.list = []; //如果是第一页需手动制空列表
					const {content} = res;
					curTab.list = [...curTab.list,...content]; //追加新数据
					curTab.curPageLen = content.length; // 当前页长
					curTab.hasNext = this.mescroll.optUp.hasNext; // 是否还有下一页
					// 需先隐藏加载状态
					this.mescroll.endSuccess(content.length);
					const endTime = new Date().getTime();
					console.log(endTime - time)
					this.loading = false;
				}).catch(err=>{
					if(!curTab.list || curTab.list.length===0) this.mescroll.endSuccess(0); 
					this.mescroll.endErr()
					this.loading = false;
				})
			},
			// 切换菜单
			tabChange (index) {
				// 记录切换前滚动条的位置
				if(!this.preIndex) this.preIndex = 0
				let preTab = this.tabs[this.preIndex]
				
				preTab.y = this.mescroll&&this.mescroll.getScrollTop()
				this.preIndex = index;
				// 当前菜单的数据
				let curTab = this.tabs[index]
				if (!curTab.list) {
					// 没有初始化,则初始化
					this.mescroll.resetUpScroll();
					if(this.mescroll.scrollTop){
						this.$nextTick(()=>{
							this.mescroll.scrollTo(this.tabTop, 0) // 恢复滚动条的位置
						})
					}
				} else{
					// 初始化过,则恢复之前的列表数据
					this.mescroll.setPageNum(curTab.num + 1); // 恢复当前页码
					this.mescroll.endSuccess(curTab.curPageLen, curTab.hasNext); // 恢复是否有下一页或显示空布局
					console.log(this.mescroll.scrollTop,this.tabTop)
					if(this.mescroll.scrollTop>this.tabTop){
						this.$nextTick(()=>{
							this.mescroll.scrollTo(top, 0) // 恢复滚动条的位置
						})
					}
				}
			},
			//侧边栏选中
			selectItem(item,val){
				const checked = item.checked.includes(val.id) ? [] : [val.id];
				this.$set(item,'checked',checked)
			},
			//重置
			resetBtn(){
				this.selectList.map(val=>{
					val.checked = [];
					return val;
				})
				this.category()
			},
			//获取选中的分类参数
			category(){
				this.categoryIds = [];
				this.form.peopleDesc = '';
				this.selectList.forEach(val=>{
					if(val.label==='人数'){
						const person = val.children.find(item=>val.checked.includes(item.id));
						if(person) this.form.peopleDesc = person.label;
					}else{
						this.categoryIds = [...val.checked,...this.categoryIds]
					}
				})
				this.show = false;
				if(Array.isArray(this.$refs.mescroll)){
					this.$refs.mescroll[this.tabIndex].selectCallback(this.categoryIds,this.form.peopleDesc)
				}else{
					this.form.categoryIds = this.categoryIds;
					this.downCallback()
				}
			},
			start(e){
				this.startData.clientX=e.changedTouches[0].clientX;
				this.startData.clientY=e.changedTouches[0].clientY;
			},
			end(e){
				const subX=e.changedTouches[0].clientX-this.startData.clientX;
				const subY=e.changedTouches[0].clientY - this.startData.clientY;
				const max = 30
				if(subY>max || subY<-max){
					if(subY>max){
						console.log('下滑')
					}else if(subY<-max){
						console.log('上滑')
					}
					return false;
				}else{
					if(subX>max){
						if(this.tabIndex===0) return false;
						--this.tabIndex;
					}else if(subX<-max){
						//左滑设置
						if(this.tabIndex+1===this.tabs.length) return false;
						++this.tabIndex;
					}else{
						return false;
						console.log('无效')
					}
					this.tabChange(this.tabIndex)
				}
			},
			getTabTop(res){
				//console.log(res,66666)
				this.tabTop = res.top - res.height;
			}
		}
	}